473,545 Members | 527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with opening and reading from a file

Helo Everyone. Im in need of some help with a C++ method. I am trying
to read entries from a MyData.dat file, and enter each of those
entries into an array.
MyArray was properly declared and MyFile was decalred as type
ifstream. I have also tried declaring it as type ofstream, both
without success. The code I have tried so far is:

int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("My Data.dat")
MyFile<<MyArray[Index].Number<<endl;
MyFile.close("M yData.dat);
}
and
int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("My Data.dat")
MyFile>>MyArray[Index].Number>>endl;
MyFile.close("M yData.dat);
}

In both cases, on checking the values of MyArray[Index], I get some
figures which are not the same as those contained in MyData file. Can
anyone please help me?
Andrew.
Jul 19 '05 #1
2 4661
Andrew wrote:
Helo Everyone. Im in need of some help with a C++ method. I am trying
to read entries from a MyData.dat file, and enter each of those
entries into an array.
MyArray was properly declared and MyFile was decalred as type
ifstream. I have also tried declaring it as type ofstream, both
without success. The code I have tried so far is:

int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("My Data.dat")
MyFile<<MyArray[Index].Number<<endl;
MyFile.close("M yData.dat);
}
The for() loop shown above (and also the one below) has a logic error.
Each time the program invokes the .open() method,

MyFile.open("My Data.dat");

it deletes the current contents of the file "MyData.dat ". So during each
iteration of the for() loop, the following events occur:

MyFile.open("My Data.dat");
// Opens the file 'MyData.dat' and deletes its contents
MyFile << MyArray[Index].Number << endl;
// Writes some data to the file 'MyData.dat'
MyFile.close();
// Closes the file 'MyData.dat'

So you should not open (or close) the data file within the body of the
for() loop.

and
int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("My Data.dat")
MyFile>>MyArray[Index].Number>>endl;
The 'endl' manipulator only works with output streams (it doesn't work
with input streams). So rewrite this line as,

MyFile >> MyArray[Index].Number;
MyFile.close("M yData.dat);
}

In both cases, on checking the values of MyArray[Index], I get some
figures which are not the same as those contained in MyData file. Can
anyone please help me?
Andrew.

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link58 09{at}now.here. com
Jul 19 '05 #2
Andrew wrote:
Helo Everyone. Im in need of some help with a C++ method. I am trying
to read entries from a MyData.dat file, and enter each of those
entries into an array.
MyArray was properly declared and MyFile was decalred as type
ifstream. I have also tried declaring it as type ofstream, both
without success. The code I have tried so far is:

int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("My Data.dat")
MyFile<<MyArray[Index].Number<<endl;
MyFile.close("M yData.dat);
}
and
int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("My Data.dat")
MyFile>>MyArray[Index].Number>>endl;
MyFile.close("M yData.dat);
}

In both cases, on checking the values of MyArray[Index], I get some
figures which are not the same as those contained in MyData file. Can
anyone please help me?
Andrew.

You open the same file 10 times. Is this what you wanted?
The file will contain the last value: MyArray[9].Number.

Or did you want something like:
unsigned index;
MyFile.open("My Data.dat");
for (index = 0; index < 10; ++index)
{
MyFile << MyArray[Index].Number << endl;
}
MyFile.close();

Some notes:
1. The C++ language is case sensitive. There is a difference
between "For" and "for".
2. The close() method does not require a filename.
3. The "endl" manipulator does not apply to an input stream.
4. Adding one to a variable (incrementing) can also be
accomplished using the increment operators.
5. Since you are using formatted output, have the program
stop after closing the file and look at it with an
editor or wordprocessor and verify the contents before
trying to read it.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
354
by: Revman | last post by:
I'm having problems opening and reading from a file to test a Class. My diver main.cpp is fairly short but with a longer file open function // Project #4 -- Main/driver program #include "daytime.h" #include <string> #include <fstream>
2
1764
by: Parker.Jim | last post by:
I need to write a program which performs word subsitutions on a text file. The program should input the names of three text files: the source file that will be "edited", a text file that contains the editing instructions, and a file that will contain the result of the editing.
7
1542
by: pillip | last post by:
I am trying to use fopen and fget to input two files and then output them into one file. Each input file has two columns and 20 rows, however since the first column in each input file is same ( numbers 0...19), i want the output file to have 3 columns and 20 rows. Right now i am using a one dimensional array "array1" to input each file:, and...
5
2981
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const...
6
4967
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a...
3
2719
by: Chi | last post by:
what is the "unable to write data to the transport connection" I use the oreilly , programming c# using System; using System.Net.Sockets; using System.Text; using System.IO; // get a file name from the client // open the file and send the // contents from the server to the client
36
3439
by: felixnielsen | last post by:
What i really wanna do, is defining my own types, it doesnt really matter why. Anyway, i have run into some problems 1) typedef unsigned short U16; U16 test = 0xffffffff; // There should be a limit on this, max value you should be able to asign should be 0xffff // std::cout << test; // result = 0xffff //
4
18523
by: slawson7 | last post by:
Easy one for all you Perl gurus out there. I'm opening a text file, reading the contents into an array, doing some processing and spitting out the results to another file. Although I've got this working fine, I had to employ a workaround when opening the file originally. I wanted to put: open(INPUT, "d:\pscripts\input.txt") or die "Could...
5
1569
by: albo | last post by:
hi all !! i need help in opening an image file (bmp) and printing it to the screen. thanks
14
1747
by: W Marsh | last post by:
Hi. In my application I do something very simple - I open a file, lock it exclusively, write some data to it and close it. If I re-open it in the same process (I mean before the script has ended) to read the file, I find that the data I have written doesn't exist in there. I can't read it back until I run the script again. My question...
0
7396
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7413
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5323
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.